<div class="card-body">
<div class="card-btns mb-2-5">
  <button class="btn btn-info" type="button" data-id="0" data-bs-toggle="modal" data-bs-target="#modal-invoice" onclick="editInvoice(this)"><i class="mdi mdi-content-save"></i> 添加开票</button>
</div>
<div class="table-responsive">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>票据类型</th>
        <th>发票号码</th>
        <th>票据抬头</th>
        <th>开票顾客名称</th>
        <th>金额(元)</th>
        <th>开票人</th>
        <th>开票时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody id="invoice_record_tbody">
        <?php foreach ($invoice_records as $k => $vo): ?>
          <tr id="invoice_tr_{:$vo['id']}">
            <td>{:$InvoiceRecordModel->getInvoiceTypeCn($vo['invoice_type'])}</td>
            <td>{:$vo['invoice_num']}</td>
            <td>{:$vo['title']}</td>
            <td>{:$vo['customer_name']}</td>
            <td>{:$vo['amount']}</td>
            <td>{:$vo['admin']['truename']}</td>
            <td>{:$vo['invoice_time']}</td>
            <td>
              <div class="btn-group btn-group-sm">
                <a class="btn btn-default" href="javascript:void(0)" data-id="{:$vo['id']}" onclick="editInvoice(this)" data-bs-toggle="modal" data-bs-target="#modal-invoice" title="编辑"><i class="mdi mdi-pencil"></i></a>
                <a class="btn btn-default" href="javascript:void(0)" data-id="{:$vo['id']}" onclick="deleteInvoice(this)" title="删除"><i class="mdi mdi-delete"></i></a>
              </div>
            </td>
          </tr>
        <?php endforeach ?>
    </tbody>
  </table>
</div>
</div>

<script type="text/javascript">
  function editInvoice(btn){
    var data_id = $(btn).attr('data-id');
    if (data_id == null) { data_id = 0; }
    $('input[name="invoice_id"]').val(data_id);

    $.ajax({
        url:'{:url("InvoiceRecord/detail")}',
        data:{id:data_id},
        type:'post',
        dataType:'json',
        success:function(data){
          var data = data.data;
          var fields = ['invoice_type','amount','customer_name','admin_id','order_id','title','invoice_code','invoice_num','payment_account','payment_bank','payment_name','payment_ratepayer','payment_address','payment_tel','invoice_time','remark'];
          for(var i in fields){
            var field = fields[i];
            $("#invoice_content").find("#"+field).val(data[field]);
          }
        }
    });
  }

  $(".submit_invoice_btn").on('click',function(){
    var params = $("#invoice_content").serialize();
    $.ajax({
        url:'{:url("InvoiceRecord/save")}',
        data:params,
        type:'post',
        dataType:'json',
        success:function(data){
          if (data.code == '2000') {
            var data = data.data;
            var str = '<tr id="invoice_tr_'+data.id+'">'+
              '<td>'+data.invoice_type_cn+'</td>'+
              '<td>'+data.invoice_num+'</td>'+
              '<td>'+data.title+'</td>'+
              '<td>'+data.customer_name+'</td>'+
              '<td>'+data.amount+'</td>'+
              '<td>'+data.admin_name+'</td>'+
              '<td>'+data.invoice_time+'</td>'+
              '<td><div class="btn-group btn-group-sm">'+
              '<a class="btn btn-default" href="javascript:void(0)" data-id="'+data.id+'" onclick="editInvoice(this)" data-bs-toggle="modal" data-bs-target="#modal-invoice" title="编辑"><i class="mdi mdi-pencil"></i></a>'+
              '<a class="btn btn-default" href="javascript:void(0)" data-id="'+data.id+'" onclick="deleteInvoice(this)" title="删除"><i class="mdi mdi-delete"></i></a>'+
              '</div></td>'+
            '</tr>';
            // 如果有ID就覆盖，没有就新增
            if ($('input[name="invoice_id"]').val() > 0) {
              $("#invoice_tr_"+data.id).replaceWith(str);
            }else{
              $("#invoice_record_tbody").append(str);
            }
          }else{
            cchUtil.message(data.msg,'danger');
          }
        }
    });
  });

  function deleteInvoice(obj){
    var data_id = $(obj).attr('data-id');
    $.ajax({
        url:'{:url("InvoiceRecord/delete")}',
        data:{id:data_id},
        type:'post',
        dataType:'json',
        success:function(data){
          if (data.code == '1') {
            $(obj).parent().parent().parent().remove();
          }else{
            cchUtil.message(data.msg,'danger');
          }
        }
    });
  }
</script>